package day001;

import java.util.Arrays;

public class Code04_InsertSort {

    public static void insertSort(int[] arr){
        if(arr == null || arr.length < 2){
            return;
        }

        int len = arr.length;
        for(int i = 0; i < len; i++){
            for(int j = i; j > 0; j--){
                if(arr[j] < arr[j-1]){
                    swap(arr, j, j-1);
                }
            }
        }

    }

    public static void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue){
        int[] arr = new int[(int)((maxSize + 1) * Math.random())];
        for(int i = 0; i < arr.length; i++){
            arr[i] = (int)((maxValue + 1)* Math.random()) - (int)(maxValue * Math.random());
        }
        return arr;
    }

    public static void compareArr(int[] arr){
        Arrays.sort(arr);
    }

    public static int[] copyArray(int[] arr){
        if(arr == null){
            return null;
        }
        int[] res = new int[arr.length];
        for(int i = 0; i < arr.length; i++){
            res[i] = arr[i];
        }
        return res;
    }

    public static void main(String[] args) {
        int testTimes = 50000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;

        for(int i = 0; i < testTimes; i++){
            int [] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            insertSort(arr1);
            compareArr(arr2);
            if(!Arrays.equals(arr1, arr2)){
                succeed = false;
                break;
            }
        }
        System.out.println(succeed ? "Nice" : "Ops");
    }
}
